package com.zhao.uitest.listview;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.zhao.uitest.BaseActivity;
import com.zhao.uitest.R;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * Created by Zhao on 2016/2/20.
 * 实现ListView的几种不同的适配器
 */
public class ListViewLayout extends BaseActivity {
    //ListView  展示的内容 各种不同的水果
    private String[] data = { "Apple", "Banana", "Orange", "Watermelon",
            "Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango" };
    //水果
    private List<Fruit> fruitList = new ArrayList<Fruit>();
    private ListView lv_main;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_layout);

        //初始化水果信息
        initFruit();
        lv_main = (ListView) findViewById(R.id.lv_main);

        /**
         * BaseAdapter
         */
        Adapter baseAdapter =  new BaseAdapter() {
            @Override
            public int getCount() {
                return data.length;
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                TextView tv;
                if (convertView!=null){
                    tv = (TextView) convertView;
                }else {
                    tv = new TextView(getApplicationContext());
                    tag("Create item #"+position);
                }
                tv.setText(data[position]);
                tv.setTextSize(40);
                return tv;
            }
        };

        /**
         * ArrayAdapter
         */
        Adapter arrayAdapter = new ArrayAdapter<String>(ListViewLayout.this,
                R.layout.simple_item, data);
        /**
         * FruitAdapter
         */
        Adapter fruitAdapter = new FruitAdpter(ListViewLayout.this, R.layout.fruit_item, fruitList);

        //为ListView设置适配器
        lv_main.setAdapter((ListAdapter) fruitAdapter);

        //设置点击监听
        lv_main.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String fruitName = fruitList.get(position).getName();
                Toast.makeText(ListViewLayout.this, fruitName, Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 初始化 水果 添加水果图片的索引 实例添加到 fruitList
     */
    private void initFruit() {
        Fruit apple = new Fruit("Apple", R.drawable.apple_pic);
        fruitList.add(apple);
        Fruit banana = new Fruit("Banana", R.drawable.banana_pic);
        fruitList.add(banana);
        Fruit orange = new Fruit("Orange", R.drawable.orange_pic);
        fruitList.add(orange);
        Fruit watermelon = new Fruit("Watermelon", R.drawable.watermelon_pic);
        fruitList.add(watermelon);
        Fruit pear = new Fruit("Pear", R.drawable.pear_pic);
        fruitList.add(pear);
        Fruit grape = new Fruit("Grape", R.drawable.grape_pic);
        fruitList.add(grape);
        Fruit pineapple = new Fruit("Pineapple", R.drawable.pineapple_pic);
        fruitList.add(pineapple);
        Fruit strawberry = new Fruit("Strawberry", R.drawable.strawberry_pic);
        fruitList.add(strawberry);
        Fruit cherry = new Fruit("Cherry", R.drawable.cherry_pic);
        fruitList.add(cherry);
        Fruit mango = new Fruit("Mango", R.drawable.mango_pic);
        fruitList.add(mango);
    }
}
